package cn.ibizlab.odoo.core.odoo_hr.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ihr_department;
import cn.ibizlab.odoo.core.odoo_hr.domain.Hr_department;
import cn.ibizlab.odoo.core.odoo_hr.filter.Hr_departmentSearchContext;
import cn.ibizlab.odoo.core.odoo_hr.service.IHr_departmentService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[HR 部门] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Hr_departmentServiceImpl implements IHr_departmentService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ihr_departmentClientService hr_departmentClientService;

    @Override
    public boolean update(Hr_department hr_department) {
        boolean bOk = false ;
        Ihr_department clientModel = this.convert2Model(hr_department,null);
        hr_departmentClientService.update(clientModel) ;
        this.convert2Domain(clientModel,hr_department) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Hr_department> hr_departments, int batchSize) {

                        //hr_departmentClientService.createBatch(hr_departments,batchSize) ;

    }

    @Override
    public void updateBatch(List<Hr_department> hr_departments, int batchSize) {

        //hr_departmentClientService.updateBatch(hr_departments,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //hr_departmentClientService.removeBatch(hr_departments) ;

    }

    @Override
    public boolean create(Hr_department hr_department) {
        boolean bOk = false ;
        Ihr_department clientModel = this.convert2Model(hr_department,null);
        hr_departmentClientService.create(clientModel) ;
        this.convert2Domain(clientModel,hr_department);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ihr_department clientModel = hr_departmentClientService.createModel();
        clientModel.setId(id);
        hr_departmentClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Hr_department get(Integer id) {
        Ihr_department clientModel = hr_departmentClientService.createModel();
        clientModel.setId(id);
        hr_departmentClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Hr_department get(Hr_department hr_department) {
        Hr_department rethr_department = this.get(hr_department.getId()) ;
    	BeanCopier copier=BeanCopier.create(rethr_department.getClass(), hr_department.getClass(), false);
        copier.copy(rethr_department, hr_department, null);
        return hr_department ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Hr_department> searchDefault(Hr_departmentSearchContext context) {
        List<Hr_department> list = new ArrayList<Hr_department>();
        Page<cn.ibizlab.odoo.core.client.model.Ihr_department> clientModelList = hr_departmentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ihr_department clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Hr_department>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据HR 部门查询
     */
    @Override
    public Page<Hr_department> selectByOdoo_parent(Integer id){
        Hr_departmentSearchContext context = new Hr_departmentSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Hr_department> list = new ArrayList<Hr_department>();
        Page<cn.ibizlab.odoo.core.client.model.Ihr_department> clientModelList = hr_departmentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ihr_department clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Hr_department>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据员工查询
     */
    @Override
    public Page<Hr_department> selectByOdoo_manager(Integer id){
        Hr_departmentSearchContext context = new Hr_departmentSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Hr_department> list = new ArrayList<Hr_department>();
        Page<cn.ibizlab.odoo.core.client.model.Ihr_department> clientModelList = hr_departmentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ihr_department clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Hr_department>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Hr_department> selectByOdoo_company(Integer id){
        Hr_departmentSearchContext context = new Hr_departmentSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Hr_department> list = new ArrayList<Hr_department>();
        Page<cn.ibizlab.odoo.core.client.model.Ihr_department> clientModelList = hr_departmentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ihr_department clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Hr_department>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Hr_department> selectByOdoo_create(Integer id){
        Hr_departmentSearchContext context = new Hr_departmentSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Hr_department> list = new ArrayList<Hr_department>();
        Page<cn.ibizlab.odoo.core.client.model.Ihr_department> clientModelList = hr_departmentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ihr_department clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Hr_department>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Hr_department> selectByOdoo_write(Integer id){
        Hr_departmentSearchContext context = new Hr_departmentSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Hr_department> list = new ArrayList<Hr_department>();
        Page<cn.ibizlab.odoo.core.client.model.Ihr_department> clientModelList = hr_departmentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ihr_department clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Hr_department>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ihr_department convert2Model(Hr_department domain , Ihr_department model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = hr_departmentClientService.createModel() ;
        }

         if(domain.getMessage_needactionDirtyFlag())
            model.setMessage_needaction(domain.getMessage_needaction());
         if(domain.getMessage_follower_idsDirtyFlag())
            model.setMessage_follower_ids(domain.getMessage_follower_ids());
         if(domain.getAbsence_of_todayDirtyFlag())
            model.setAbsence_of_today(domain.getAbsence_of_today());
         if(domain.getMessage_unreadDirtyFlag())
            model.setMessage_unread(domain.getMessage_unread());
         if(domain.getLeave_to_approve_countDirtyFlag())
            model.setLeave_to_approve_count(domain.getLeave_to_approve_count());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getMessage_partner_idsDirtyFlag())
            model.setMessage_partner_ids(domain.getMessage_partner_ids());
         if(domain.getMessage_main_attachment_idDirtyFlag())
            model.setMessage_main_attachment_id(domain.getMessage_main_attachment_id());
         if(domain.getMessage_unread_counterDirtyFlag())
            model.setMessage_unread_counter(domain.getMessage_unread_counter());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getMessage_is_followerDirtyFlag())
            model.setMessage_is_follower(domain.getMessage_is_follower());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getExpense_sheets_to_approve_countDirtyFlag())
            model.setExpense_sheets_to_approve_count(domain.getExpense_sheets_to_approve_count());
         if(domain.getAllocation_to_approve_countDirtyFlag())
            model.setAllocation_to_approve_count(domain.getAllocation_to_approve_count());
         if(domain.getNew_hired_employeeDirtyFlag())
            model.setNew_hired_employee(domain.getNew_hired_employee());
         if(domain.getNoteDirtyFlag())
            model.setNote(domain.getNote());
         if(domain.getChild_idsDirtyFlag())
            model.setChild_ids(domain.getChild_ids());
         if(domain.getMessage_channel_idsDirtyFlag())
            model.setMessage_channel_ids(domain.getMessage_channel_ids());
         if(domain.getMessage_has_error_counterDirtyFlag())
            model.setMessage_has_error_counter(domain.getMessage_has_error_counter());
         if(domain.getExpected_employeeDirtyFlag())
            model.setExpected_employee(domain.getExpected_employee());
         if(domain.getWebsite_message_idsDirtyFlag())
            model.setWebsite_message_ids(domain.getWebsite_message_ids());
         if(domain.getMessage_has_errorDirtyFlag())
            model.setMessage_has_error(domain.getMessage_has_error());
         if(domain.getTotal_employeeDirtyFlag())
            model.setTotal_employee(domain.getTotal_employee());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getJobs_idsDirtyFlag())
            model.setJobs_ids(domain.getJobs_ids());
         if(domain.getMessage_idsDirtyFlag())
            model.setMessage_ids(domain.getMessage_ids());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getMessage_attachment_countDirtyFlag())
            model.setMessage_attachment_count(domain.getMessage_attachment_count());
         if(domain.getNew_applicant_countDirtyFlag())
            model.setNew_applicant_count(domain.getNew_applicant_count());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getComplete_nameDirtyFlag())
            model.setComplete_name(domain.getComplete_name());
         if(domain.getMessage_needaction_counterDirtyFlag())
            model.setMessage_needaction_counter(domain.getMessage_needaction_counter());
         if(domain.getMember_idsDirtyFlag())
            model.setMember_ids(domain.getMember_ids());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getManager_id_textDirtyFlag())
            model.setManager_id_text(domain.getManager_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getParent_id_textDirtyFlag())
            model.setParent_id_text(domain.getParent_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getManager_idDirtyFlag())
            model.setManager_id(domain.getManager_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getParent_idDirtyFlag())
            model.setParent_id(domain.getParent_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
        return model ;
    }

    public Hr_department convert2Domain( Ihr_department model ,Hr_department domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Hr_department();
        }

        if(model.getMessage_needactionDirtyFlag())
            domain.setMessage_needaction(model.getMessage_needaction());
        if(model.getMessage_follower_idsDirtyFlag())
            domain.setMessage_follower_ids(model.getMessage_follower_ids());
        if(model.getAbsence_of_todayDirtyFlag())
            domain.setAbsence_of_today(model.getAbsence_of_today());
        if(model.getMessage_unreadDirtyFlag())
            domain.setMessage_unread(model.getMessage_unread());
        if(model.getLeave_to_approve_countDirtyFlag())
            domain.setLeave_to_approve_count(model.getLeave_to_approve_count());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getMessage_partner_idsDirtyFlag())
            domain.setMessage_partner_ids(model.getMessage_partner_ids());
        if(model.getMessage_main_attachment_idDirtyFlag())
            domain.setMessage_main_attachment_id(model.getMessage_main_attachment_id());
        if(model.getMessage_unread_counterDirtyFlag())
            domain.setMessage_unread_counter(model.getMessage_unread_counter());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getMessage_is_followerDirtyFlag())
            domain.setMessage_is_follower(model.getMessage_is_follower());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getExpense_sheets_to_approve_countDirtyFlag())
            domain.setExpense_sheets_to_approve_count(model.getExpense_sheets_to_approve_count());
        if(model.getAllocation_to_approve_countDirtyFlag())
            domain.setAllocation_to_approve_count(model.getAllocation_to_approve_count());
        if(model.getNew_hired_employeeDirtyFlag())
            domain.setNew_hired_employee(model.getNew_hired_employee());
        if(model.getNoteDirtyFlag())
            domain.setNote(model.getNote());
        if(model.getChild_idsDirtyFlag())
            domain.setChild_ids(model.getChild_ids());
        if(model.getMessage_channel_idsDirtyFlag())
            domain.setMessage_channel_ids(model.getMessage_channel_ids());
        if(model.getMessage_has_error_counterDirtyFlag())
            domain.setMessage_has_error_counter(model.getMessage_has_error_counter());
        if(model.getExpected_employeeDirtyFlag())
            domain.setExpected_employee(model.getExpected_employee());
        if(model.getWebsite_message_idsDirtyFlag())
            domain.setWebsite_message_ids(model.getWebsite_message_ids());
        if(model.getMessage_has_errorDirtyFlag())
            domain.setMessage_has_error(model.getMessage_has_error());
        if(model.getTotal_employeeDirtyFlag())
            domain.setTotal_employee(model.getTotal_employee());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getJobs_idsDirtyFlag())
            domain.setJobs_ids(model.getJobs_ids());
        if(model.getMessage_idsDirtyFlag())
            domain.setMessage_ids(model.getMessage_ids());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getMessage_attachment_countDirtyFlag())
            domain.setMessage_attachment_count(model.getMessage_attachment_count());
        if(model.getNew_applicant_countDirtyFlag())
            domain.setNew_applicant_count(model.getNew_applicant_count());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getComplete_nameDirtyFlag())
            domain.setComplete_name(model.getComplete_name());
        if(model.getMessage_needaction_counterDirtyFlag())
            domain.setMessage_needaction_counter(model.getMessage_needaction_counter());
        if(model.getMember_idsDirtyFlag())
            domain.setMember_ids(model.getMember_ids());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getManager_id_textDirtyFlag())
            domain.setManager_id_text(model.getManager_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getParent_id_textDirtyFlag())
            domain.setParent_id_text(model.getParent_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getManager_idDirtyFlag())
            domain.setManager_id(model.getManager_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getParent_idDirtyFlag())
            domain.setParent_id(model.getParent_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        return domain ;
    }


}
